﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OpenSoftStore.Orm.Model;
using OpenSoftStore.Orm.View;
using OpenSoftStore.BusinessLogic.Report;
using OpenSoftStore.Client.Common;

namespace OpenSoftStore.Client.UIL.ManageReport
{
    public partial class ReportBorakerage : UserControl
    {
        private T_Admin admin;
        private List<V_Admin> adminView;
        private ReportLogic reportBll;
        private static ReportBorakerage instance;
        public static ReportBorakerage Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new ReportBorakerage();
                }
                return instance;
            }
        }

        public ReportBorakerage()
        {
            InitializeComponent();
            admin = new T_Admin();
            adminView = new List<V_Admin>();
            reportBll = new ReportLogic();
            dtpFrom.Value = DateTime.Now.Date;
            dtpTo.Value = DateTime.Now.Date.AddDays(1.0).AddSeconds(-1.0);
            var from = dtpFrom.Value;
            var to = dtpTo.Value;
            dgvBrokGroup.AutoGenerateColumns = false;
            dgvBrokGroup.DataSource = reportBll.GetBroNumReport(" where brok.AddTime between #" + from + "# and #" + to + "#");
            dgvBrokerageItem.AutoGenerateColumns = false;
            dgvBrokerageItem.DataSource = reportBll.GetBroNumDetailReport(" where brok.AddTime between #" + from + "# and #" + to + "#");
            SPACommon.SetDataGridStyle(dgvBrokGroup);
            SPACommon.SetDataGridStyle(dgvBrokerage);
            SPACommon.SetDataGridStyle(dgvBrokerageItem);
            SPACommon.ShowSummary(dgvBrokerageItem);
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            var from = dtpFrom.Value.Date;
            var to = dtpTo.Value.Date.AddDays(1).AddSeconds(-1);
            dgvBrokGroup.AutoGenerateColumns = false;
            dgvBrokGroup.DataSource = reportBll.GetBroNumReport(" where brok.AddTime between #" + from + "# and #" + to + "#");
            dgvBrokerageItem.AutoGenerateColumns = false;
            dgvBrokerageItem.DataSource = reportBll.GetBroNumDetailReport(" where brok.AddTime between #" + from + "# and #" + to + "#");
        }

        private void dgvBrokGroup_SelectionChanged(object sender, EventArgs e)
        {
            var from = dtpFrom.Value.Date;
            var to = dtpTo.Value.Date.AddDays(1).AddSeconds(-1);
            string adminId = dgvBrokGroup.CurrentRow.Cells["AdminId"].Value.ToString();
            string whereStr = string.Format(" where A.Id = '{0}' and  brok.AddTime between #{1}# and #{2}#", adminId, from, to);
            dgvBrokerage.AutoGenerateColumns = false;
            dgvBrokerage.DataSource = reportBll.GetBroNumDetailReport(whereStr);
        }

        private void ReportBorakerage_Load(object sender, EventArgs e)
        {

        }

        private void btnOutput_Click(object sender, EventArgs e)
        {
            SPACommon.ExportToExcel(dgvBrokGroup, "人员提成统计");
            SPACommon.ExportToExcel(dgvBrokerageItem, "提成项统计");
        }
    }
}
